package com.zzyy.study.controller;

import com.zzyy.study.service.ChatAssistant;
import dev.langchain4j.data.message.AiMessage;
import dev.langchain4j.data.message.ImageContent;
import dev.langchain4j.data.message.TextContent;
import dev.langchain4j.data.message.UserMessage;
import dev.langchain4j.model.StreamingResponseHandler;
import dev.langchain4j.model.chat.ChatLanguageModel;
import dev.langchain4j.model.chat.StreamingChatLanguageModel;
import dev.langchain4j.model.output.Response;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.http.server.ServletServerHttpResponse;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.server.adapter.DefaultServerWebExchange;
import reactor.core.publisher.Flux;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.concurrent.TimeUnit;

/**
 * @auther zzyy
 * @create 2025-02-21 22:44
 */
@RestController
@Slf4j
public class ChatLanguageModelController
{
    @Resource
    private StreamingChatLanguageModel streamingChatLanguageModel;
    @Resource
    private ChatAssistant chatAssistant;


    // http://localhost:9005/chatstream/chat2?prompt=%E6%B9%96%E5%8C%97%E6%9C%89%E4%BB%80%E4%B9%88%E5%A5%BD%E5%90%83%E7%9A%84
    @GetMapping(value = "/chatstream/chat2")
    public Flux<String> chat2(@RequestParam("prompt") String prompt, HttpServletResponse response) {
        // 设置响应的字符编码
        response.setCharacterEncoding("UTF-8");

        chatAssistant.chat2(prompt).subscribe(
                System.out::println,  // onNext
                Throwable::printStackTrace,  // onError
                () -> System.out.println("onComplete")  // onComplete
        );

        /*chatAssistant.chat2(prompt).subscribe(
                str -> {
                    // 这里的代码会在每次有新的String时执行
                    System.out.println(str);
                },
                error -> {
                    // 这里的代码会在发生错误时执行
                    System.err.println("Error occurred: " + error.getMessage());
                },
                () -> {
                    // 这里的代码会在Flux完成时执行
                    System.out.println("All strings processed onComplete.");
                }
        );*/

        //暂停几秒钟线程
        try { TimeUnit.SECONDS.sleep(7); } catch (InterruptedException e) { e.printStackTrace(); }

        return chatAssistant.chat2(prompt);
    }

    @GetMapping(value = "/chatstream/assistant2")
    public void assistant2(@RequestParam(value = "prompt", defaultValue = "hello, 北京有什么好吃的？") String prompt)
    {
        streamingChatLanguageModel.generate(prompt, new StreamingResponseHandler<AiMessage>(){
            @Override
            public void onNext(String token) {
                System.out.println(token);
            }

            @Override
            public void onError(Throwable error) {

            }
        });
        //暂停几秒钟线程
        try { TimeUnit.SECONDS.sleep(20); } catch (InterruptedException e) { e.printStackTrace(); }
    }
}
